package com.blog.wl2023.repository;

import com.blog.wl2023.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wule
 * @data 2023年08月15日22时
 * @description 与用户相关的sql方法
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

	@Query("SELECT new User(u.uuid,u.email,u.password,u.username,u.imageUrl,u.gender,u.introduction,u.createTime,u.status) FROM User u WHERE u.email = :email")
	User findUserByEmail(String email);

	@Modifying
	@Transactional
	@Query("update User u set u.code=:code where u.email=:oldEmail")
	void updateCode(String oldEmail, String code);

	@Modifying
	@Transactional
	@Query("update User u set u.imageUrl=:fileName where u.uuid=:uuid")
	void updateUserAvatar(String uuid, String fileName);

	@Modifying
	@Transactional
	@Query("update User u set u.username=:username,u.gender=:gender,u.introduction=:introduction where u.uuid=:uuid")
	void updateUserInfo(String uuid, String username, String gender, String introduction);

	@Query("SELECT new User(u.uuid,u.email,u.username,u.imageUrl,u.gender,u.introduction,u.createTime)  FROM  User u WHERE  u.uuid = :uuid")
	User findUserByUUid(String uuid);
}